/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ui.user.personal;

import hibernate.dao.AccountDAO;
import hibernate.dao.ResetPasswordDAO;
import hibernate.entity.Accounts;
import hibernate.entity.ResetRequests;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import javax.mail.MessagingException;
import javax.mail.internet.AddressException;
import ui.common.AbstractUIBean;
import ui.common.Login;
import ui.user.GoogleMail;
import util.RandomGenerator;

/**
 *
 * @author Neo
 */
@ManagedBean
@SessionScoped
public class ResetPassword extends AbstractUIBean {

    boolean isValid;
    Accounts requestAccount = null;
    ResetPasswordDAO rpDao = new ResetPasswordDAO();
    String email;
    String newPass;
    boolean sent;

    public ResetPassword() {
        sent = false;
    }

    public boolean isIsValid() {
        //HttpServletRequest request = (HttpServletRequest) exContext.getRequest();
        String _genCode = request.getParameter("gencode") != null ? request.getParameter("gencode") : "";
        String _email = request.getParameter("email") != null ? request.getParameter("email") : "";
        if (rpDao.getByCodeAndEmail(_genCode, _email) != null) {
            sessionMap.put("rsEmail", email);
            return true;
        }
        return false;
    }

    public boolean isSent() {
        return sent;
    }

    public void setSent(boolean sent) {
        this.sent = sent;
    }

    public String checkAndSend() {
        requestAccount = new AccountDAO().readByEmail(email);
        if (requestAccount != null) {
            sendRecover(requestAccount);
        }
        return null;
    }

    public void sendRecover(Accounts acc) {
        try {
            String gencode = RandomGenerator.randomString(30);
            String content = "http://localhost:8080/mailserver/faces/user/personal/resetpassword.xhtml?gencode=" + gencode
                    + "&email=" + email;
            GoogleMail.Send("mailserverbtl6", "iammailserver", email, "Reset password", content);
//            requestAccount = new AccountDAO().readByEmail(email);
            ResetRequests rq = new ResetRequests(acc, gencode);
            rpDao.create(rq);
            setSent(true);
        } catch (AddressException ex) {
            Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MessagingException ex) {
            Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
//            setIsError(true);
//            MSG_ERROR = ex.getMessage();
        }
    }

    public void doReset() {
        String rsEmail = String.valueOf(sessionMap.get("rsEmail"));
        int r = rpDao.resetPassword(rsEmail, newPass);
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getNewPass() {
        return newPass;
    }

    public void setNewPass(String newPass) {
        this.newPass = newPass;
    }
}
